package com.to.aboomy.bannersample.indicator;

import com.to.aboomy.banner.Indicator;
import com.to.aboomy.bannersample.slice.Indicator2AbilitySclice;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * 横线
 *
 * @since 2021-08-06
 */
public class LineIndicatorView extends DirectionalLayout implements Indicator {
    private Context context;
    private int pointRadius  = 10;
    private int pointSelectColor = Color.RED.getValue();
    private int pointWidth = pointRadius * 6;
    private int pointHeight = pointRadius * 2;
    private int spacing = pointRadius;
    private DirectionalLayout directionalLayout;
    private int pagerCount;
    public LineIndicatorView(Context context) {
        super(context);
        this.context = context;
        setOrientation(DirectionalLayout.HORIZONTAL);
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(RgbColor.fromArgbInt(0xFFCCCCCC));
        setBackground(shapeElement);
    }

    public LineIndicatorView(Context context, AttrSet attrSet) {
        super(context, attrSet);
    }

    public LineIndicatorView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
    }

    @Override
    public void initIndicatorCount(int pagerCount) {
        this.pagerCount=pagerCount;
        setVisibility(pagerCount > 1 ? VISIBLE : INVISIBLE);
        removeAllComponents();
        DirectionalLayout.LayoutConfig lp = new DirectionalLayout.LayoutConfig(pagerCount*pointWidth, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        directionalLayout = new DirectionalLayout(getContext());
        directionalLayout.setOrientation(DirectionalLayout.HORIZONTAL);
        directionalLayout.setLayoutConfig(lp);
            // 翻页指示的点
         Text pointView = new Text(getContext());
         ShapeElement drawable = new ShapeElement();
         pointView.setWidth(pointWidth);
         drawable.setRgbColor(RgbColor.fromArgbInt(0XFF40C4FF));
        drawable.setCornerRadius(8);
         pointView.setSelected(false);
         pointView.setHeight(pointHeight);
         pointView.setBackground(drawable);
         directionalLayout.addComponent(pointView);
         addComponent(directionalLayout);
    }

    @Override
    public Component getView() {
        return this;
    }
    DependentLayout.LayoutConfig params = null;
    @Override
    public DependentLayout.LayoutConfig getParams() {
        if (params == null) {
            params = new DependentLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT, 20);
            params.addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_BOTTOM);
            params.addRule(DependentLayout.LayoutConfig.CENTER_IN_PARENT);
            params.addRule(DependentLayout.LayoutConfig.BELOW);
            params.setMarginBottom(AttrHelper.vp2px(10, context));
        }
        return params;
    }

    @Override
    public void setAutoStau(Boolean isAuto) {

    }

    @Override
    public void onPageSliding(int i, float v, int i1) {

    }

    @Override
    public void onPageSlideStateChanged(int i) {

    }

    @Override
    public void onPageChosen(int position) {
        selectPoint(position);
    }
    public void selectPoint(int position) {
        final Text pointView = (Text) directionalLayout.getComponentAt(0);
        if (position >= 0 && position < pagerCount) {
            selectPoint(pointView,position);
        }
    }

    private void selectPoint2(final Text v) {
        if (v != null && !v.isSelected()) {
            v.setTranslationX(0);
        }
    }

    private void selectPoint(final Text v,int position) {
        if (v != null) {
            final float temp = pointWidth;
            AnimatorValue animatorValue = new AnimatorValue();
            animatorValue.setDuration(300);
            animatorValue.setValueUpdateListener((animatorValue1, v1) -> {
                // 等于当前时间减去动画开始的时间。
                int startx = pointWidth*(position-1);
                float StraX = startx + (v1 * temp);
                v.setTranslationX(StraX);
            });
            animatorValue.start();
        }
    }

}
